package com.ndp.fb.rdb.mongo.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.StrategyRuleMongoService;
import com.ndp.fb.mongo.model.autocreatead.robot.StrategyRule;
@Service
public class StrategyRuleMongoServiceImpl implements StrategyRuleMongoService {
	private static Logger logger = LoggerFactory.getLogger(StrategyRuleMongoServiceImpl.class);

	@Autowired
	private DB adResourceMongoDBClientPrimary;

	@Override
	public void save(StrategyRule rule) {
		try {
			DBObject dbObject = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(rule));
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			dbCollection.update(new BasicDBObject("id", dbObject.get("id")),dbObject,true,false,WriteConcern.JOURNALED);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		

	}

	@Override
	public StrategyRule getById(String id) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			DBObject dbObject = dbCollection.findOne(new BasicDBObject("id", id));
			return JSONObject.parseObject(JSONObject.toJSONString(dbObject), StrategyRule.class);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public List<StrategyRule> getAll() {
		List<StrategyRule> rules = new ArrayList<>();
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			DBCursor cursor = dbCollection.find();
			while (cursor.hasNext()) {
				rules.add(JSONObject.parseObject(JSONObject.toJSONString(cursor.next()), StrategyRule.class));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return rules;
	}

	@Override
	public StrategyRule scoreForGtNext(Integer score) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			DBCursor cursor = dbCollection.find(new BasicDBObject("score", new BasicDBObject("$gt", score))).limit(1).sort(new BasicDBObject("score", 1));
			if(cursor!=null&&cursor.hasNext()){
				return JSONObject.parseObject(JSONObject.toJSONString(cursor.next()), StrategyRule.class);
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public StrategyRule scoreForLeNext(Integer score) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			DBCursor cursor = dbCollection.find(new BasicDBObject("score", new BasicDBObject("$lt", score))).limit(1).sort(new BasicDBObject("score", -1));
			if(cursor!=null&&cursor.hasNext()){
				return JSONObject.parseObject(JSONObject.toJSONString(cursor.next()), StrategyRule.class);
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public StrategyRule scoreForMax() {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			DBCursor cursor = dbCollection.find().limit(1).sort(new BasicDBObject("score", -1));
			if(cursor!=null&&cursor.hasNext()){
				return JSONObject.parseObject(JSONObject.toJSONString(cursor.next()), StrategyRule.class);
			}
			return null;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	@Override
	public int getRuleCountLeScore(Integer score) {
		try {
			DBCollection dbCollection = adResourceMongoDBClientPrimary
					.getCollection(MongoConstants.TABLE_NAME_STRATEGYRULE);
			Long count = dbCollection.count(new BasicDBObject("score", new BasicDBObject("$lt", score)));
			return count.intValue();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return 0;
	}

}
